(function(_,$){
	_.Html.link = "<div class='jdmer_link' onselectstart='return false;' title=''>"+
	"		<img draggable='false' src=''>"+
	"		<div class='label'></div>" +
	"		<div class='lucency'></div>" +
	"		<div class='notify'><span></span></div>"+
	"	</div>";
	
	var Link = function(_config){
		var _this = this;
		this.uid = _.Util.guid();	//唯一编号
		var config = {
			id : null,
			ico : null,
			icoClass : "jdmer_link_default_ico",
			label : "快捷方式",
			top : 0,
			left : 0,
			container : "body",
			notify : 0
		};
		$.extend(config,_config);
		
		this.jQuery = null;
		var jq = {
			obj : null,
			ico : null,
			label : null,
			notify : null
		};
		var htmlBase = _.Html.link;	//html原型
		
		/**
		 * 显示当前对象
		 */
		this.display = function(){
			initjQuery();
			jq.obj.attr("jdmer_uid",_this.uid);
			_this.id(config.id);
			_this.container(config.container);
			_this.icoClass(config.icoClass);
			_this.ico(config.ico);
			_this.label(config.label);
			_this.top(config.top);
			_this.left(config.left);
			_this.notify(config.notify);
			jq.obj.data("jDreamerObject",_this);
			return this;
		};
		
		/**
		 * 设置id属性
		 */
		this.id = function(_id){
			if(_id == undefined){
				return config.id;
			}else{
				config.id = _id;
				if(jq.obj && config.id){
					jq.obj.attr("id",config.id);
				}
				return this;
			}
		};
		
		/**
		 * 设置top值
		 * @param	_top	top值
		 */
		this.top = function(_top){
			if(_top == undefined){
				return config.top;
			}else{
				config.top = _top;
				if(jq.obj){
					jq.obj.css("top",config.top);
					jq.obj.attr("jdmer_top",config.top);
				}
				return this;
			}
		};
		
		/**
		 * 设置提示数量
		 */
		this.notify = function(_notify){
			if(_notify == undefined){
				return config.notify;
			}else{
				config.notify = _notify;
				if(jq.obj){
					var sp = jq.notify.children("span");
					sp.html(config.notify);
					if(config.notify == 0){
						sp.hide();
					}else{
						sp.show();
					}
				}
				return this;
			}
		};
		
		/**
		 * 设置left值
		 * @param	_left	left值
		 */
		this.left = function(_left){
			if(_left == undefined){
				return config.left;
			}else{
				config.left = _left;
				if(jq.obj){
					jq.obj.css("left",config.left);
					jq.obj.attr("jdmer_left",config.left);
				}
				return this;
			}
		};
		
		this.label = function(_label){
			if(_label == undefined){
				return config.label;
			}else{
				config.label = _label;
				if(config.label && jq.obj){
					jq.obj.attr("title",config.label);
					jq.label.html(config.label);
				}
				return this;
			};
		};
		
		/**
		 * 通过样式名设置图标
		 * @param _icoClass	包含图标的css样式
		 */
		this.icoClass = function(_icoClass){
			if(_icoClass == undefined){
				return config.icoClass;
			}else{
				config.icoClass = _icoClass;
				if(config.icoClass && jq.obj){
					var img = jq.obj.getImageFromClass(config.icoClass);
					_this.ico(img);
				}
				return this;
			}
		};
		
		/**
		 * 设置图标
		 * @param _ico	图标文件路径
		 */
		this.ico = function(_ico){
			if(_ico == undefined){
				return config.ico;
			}else{
				config.ico = _ico;
				if(config.ico && jq.obj){
					jq.ico.attr("src",config.ico);
				}
				return this;
			}
		};
		
		/**
		 * 设置容器
		 * @param _container	容器
		 */
		this.container = function(_container){
			if(_container == undefined){
				return $(config.container);
			}else{
				config.container = _container;
				if(jq.obj){
					jq.obj.appendTo($(config.container));
				}
				return this;
			}
		};
		
		/**
		 * 初始化jQuery对象
		 */
		var initjQuery = function(){
			jq.obj = $(htmlBase);
			_this.jQuery = jq.obj;
			jq.ico = jq.obj.children("img");
			jq.label = jq.obj.children(".label");
			jq.notify = jq.obj.children(".notify");
			
			jq.obj.appendTo($(config.container));
			
			jq.obj.mouseoverout("jdmer_link_hover");
		};
	};
	_.Link = Link;
	_.link = function(_config){
		return new _.Link(_config);
	};
	
	if(!_.Changer){
		return ;
	}
	_.Changer.addHandler({
		name : "link",
		selector : "div[changer='link']",
		handler : function(objs , region){
			objs.each(function(i){
				var current = objs.eq(i);
				var container = current.parent();
				var ctnWidth = container.width();
				var ctnHeight = container.height();
				var config = current.attr("config");
				if(config){ config = eval("("+config+")"); }else{ config = {}; }	//转换config
				var label = current.html();
				if(label){config.label = label;}
				var link = _.link(config);
				link.display();
				link.jQuery.insertAfter(current);
				var target = current.attr("target");
				
				if(target){
					var windowConfig = current.attr(target);
					if(windowConfig){ windowConfig = eval("("+windowConfig+")"); }else{ windowConfig = {}; }	//转换windowConfig
					
					windowConfig.title = config.label;
					windowConfig.container = container;
					
					var win = {
						jQuery : null
					};
					link.jQuery.dblclick(function(){
						if(win.jQuery == null){
							win = _.window(windowConfig);
							ctnWidth = container.width();
							ctnHeight = container.height();
							win.top((ctnHeight - win.height())/2);
							win.left((ctnWidth - win.width())/2);
						}
						win.display();
					});
				}
				
				current.remove();
			});	
		}
	});
	
})(jDreamer,jQuery);